package com.woniuxy.orderserver.listener;

import com.alibaba.fastjson2.JSONObject;
import com.woniuxy.common.dto.SkuDto;
import com.woniuxy.common.dto.UserDto;
import com.woniuxy.orderserver.service.FinanceOrderService;
import lombok.AllArgsConstructor;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.woniuxy.common.constant.FinanceConstant.RabbitMq.*;

@Component
@AllArgsConstructor
public class OrderListener {
    public static final String ORDER_QUEUE = "order.queue";
    @Autowired
    private FinanceOrderService orderService;

    @RabbitListener(queues = ORDER_QUEUE)
    public void addOrder(String json){
        JSONObject jsonObject = JSONObject.parseObject(json);
        Long orderId = jsonObject.getLong(ORDER_ID);
        SkuDto sku = jsonObject.getObject(SKU, SkuDto.class);
        UserDto user = jsonObject.getObject(USER, UserDto.class);
        orderService.addOrder(user,orderId,sku);
    }

    @RabbitListener(queues = "dlx.queue")
    public void rollbackOrder(String orderId){
        orderService.rollbackOrder(orderId);
    }
}
